package com.aaa.config;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;

import org.springframework.boot.context.properties.bind.Binder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.data.redis.connection.RedisStandaloneConfiguration;
import org.springframework.data.redis.connection.jedis.JedisClientConfiguration;
import org.springframework.data.redis.connection.jedis.JedisConnectionFactory;

@Configuration
public class MyRedisConfig {

    //加载配置文件到配置类中
    @Bean
    public MyRedisProperties myRedisProperties(Environment environment){
        MyRedisProperties myRedisProperties = Binder.get(environment).bind(MyRedisProperties.PREFIX, MyRedisProperties.class).get();

        return myRedisProperties;
    }

    //连接工程 用来获取redis连接对象
    // standaloneConfig   单机redis配置
    // clientConfig       jedis的配置
    @Bean
    public JedisConnectionFactory   jedisConnectionFactory(RedisStandaloneConfiguration standaloneConfig, JedisClientConfiguration clientConfig){
        JedisConnectionFactory factory = new JedisConnectionFactory(standaloneConfig,clientConfig);
        return factory;
    }

    // redis单机配置
    @Bean
    public RedisStandaloneConfiguration redisStandaloneConfiguration(MyRedisProperties   properties){
        RedisStandaloneConfiguration redisStandaloneConfiguration = new RedisStandaloneConfiguration();
        redisStandaloneConfiguration.setHostName(properties.getHost());
        redisStandaloneConfiguration.setPort(properties.getPort());
        redisStandaloneConfiguration.setPassword(properties.getPassword());
        redisStandaloneConfiguration.setDatabase(properties.getDatabase());

        return redisStandaloneConfiguration;
    }

    //jedis配置
    @Bean
    public JedisClientConfiguration jedisClientConfiguration(GenericObjectPoolConfig   genericObjectPoolConfig){

        JedisClientConfiguration.JedisPoolingClientConfigurationBuilder builder = (JedisClientConfiguration.JedisPoolingClientConfigurationBuilder) JedisClientConfiguration.builder();

        JedisClientConfiguration.JedisPoolingClientConfigurationBuilder jedisPoolingClientConfigurationBuilder = builder.poolConfig(genericObjectPoolConfig);

        JedisClientConfiguration build = jedisPoolingClientConfigurationBuilder.build();

        return build;
    }

    @Bean
    public GenericObjectPoolConfig genericObjectPoolConfig(MyRedisProperties   properties){
        GenericObjectPoolConfig genericObjectPoolConfig = new GenericObjectPoolConfig();

        genericObjectPoolConfig.setMaxWaitMillis(properties.getMaxWaitMillis());
        genericObjectPoolConfig.setMaxTotal(properties.getMaxTotal());
        genericObjectPoolConfig.setMinIdle(properties.getMinIdle());
        genericObjectPoolConfig.setMaxIdle(properties.getMaxIdle());
        genericObjectPoolConfig.setEvictorShutdownTimeoutMillis(properties.getTimeout());

        return genericObjectPoolConfig;

    }
}
